package com.china08.yxyapi.api.manage.sch.platform.website.section;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.entity.db.sws.SwsSection.DisplayStyle;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController("SectionCrtController")
public class SectionCrtController extends YxyApiBaseController {

    @Autowired
    SwsSectionRepository swsSectionRepository;

    @ApiOperation("创建栏目")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/section/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        // 当前用户学校
        String pid = reqModel.pid;
        String outLink = reqModel.outLink;
        boolean isAudit = reqModel.isAudit;
        String sectionName = reqModel.sectionName;
        Integer sectionType = reqModel.sectionType;
        String displayStyle = reqModel.displayStyle;
        boolean showInHomePage = reqModel.showInHomePage;
        boolean showNavigation = reqModel.showNavigation;
        boolean showNavigationForMicroWebSite = reqModel.showNavigationForMicroWebSite;
        boolean isOutLink = false;
        if (sectionType == 1) {
            isOutLink = true;
        }
        String schoolId = getProfile(request, response).getSchoolId();

        // 判断栏目是否存在
        List<SwsSection> list = swsSectionRepository.findBySchoolIdAndSectionName(schoolId, sectionName);
        Assert.isTrue(list.size() == 0, "当前栏目已经存在了");

        Integer orderNum = 1;

        if (pid.equals("0")) {
            Assert.isTrue(sectionName.length() > 1 && sectionName.length() < 6, "请输入2-5个字的栏目名称");
            // 一级栏目总数量
            Integer count = swsSectionRepository.countBySchoolIdAndPid(schoolId, "0");
            Assert.isTrue(count < 20, "最多可以添加20个一级栏目");

            // 获取排序字段
            SwsSection swsSection = swsSectionRepository.findTop1BySchoolIdAndPidOrderByOrderNumDesc(schoolId, "0");
            orderNum = (swsSection == null) ? 1 : swsSection.getOrderNum() + 1;
        } else {

            Assert.isTrue(sectionName.length() > 1 && sectionName.length() < 7, "请输入2-6个字的栏目名称");
            // 一级栏目总数量
            Integer count = swsSectionRepository.countBySchoolIdAndPid(schoolId, pid);
            Assert.isTrue(count < 20, "最多可以添加20个二级栏目");

            // 获取排序字段
            SwsSection swsSection = swsSectionRepository.findTop1BySchoolIdAndPidOrderByOrderNumDesc(schoolId, pid);
            orderNum = (swsSection == null) ? 1 : swsSection.getOrderNum() + 1;
        }

        SwsSection section = new SwsSection();
        section.setId(UUID.randomUUID().toString().replace("-", ""));
        section.setSchoolId(schoolId);
        section.setSectionName(sectionName);
        section.setSectionType(sectionType);
        section.setPid(pid);
        section.setIsAudit(isAudit);
        section.setShowInHomePage(showInHomePage);
        section.setShowNavigation(showNavigation);
        // 是外链行
        if (isOutLink) {
            section.setShowNavigationForMicroWebSite(false);
        } else {
            section.setShowNavigationForMicroWebSite(showNavigationForMicroWebSite);
        }
        section.setOutLink(outLink);
        section.setIsOutLink(isOutLink);
        section.setOrderNum(orderNum);
        if (displayStyle.equals("TEXT_LIST")) {
            section.setDisplayStyle(DisplayStyle.TEXT_LIST);
        } else if (displayStyle.equals("IMG_LIST")) {
            section.setDisplayStyle(DisplayStyle.IMG_LIST);
        } else {
            section.setDisplayStyle(DisplayStyle.IMG_GRID);
        }

        // 保存
        swsSectionRepository.save(section);
        return true;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 栏目名称
        public String sectionName;

        // 栏目类型
        public Integer sectionType;

        // 栏目关联id
        public String pid;

        // 是否首页显示
        public boolean showInHomePage;

        // 是否导航显示
        public boolean showNavigation;

        public boolean showNavigationForMicroWebSite;

        // 展示样式
        public String displayStyle;

        // 是否审核
        public boolean isAudit;

        // 外链接
        public String outLink;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(sectionName), "栏目名称不能为空");

            // 外链行
            if (sectionType == 1) {
                boolean b1 = StringUtils.isNotBlank(outLink) && outLink.startsWith("http://");
                boolean b2 = StringUtils.isNotBlank(outLink) && outLink.startsWith("https://");
                Assert.isTrue(b1 || b2, "请以http://或https://开头");
                showInHomePage = false;
            } //
            else {
                outLink = null;
                Assert.isTrue(StringUtils.isNotBlank(displayStyle), "列表展示样式不能为空");
            }
        }
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
